﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.EArrangeD
{
    public class EArrangeDAL
    {
        private readonly StarsEASContext _context;
        public EArrangeDAL(StarsEASContext context) { 
            _context = context;
        }

        public ResultUtils GetAllEArrange() {
            StarsEASContext _context = new StarsEASContext();
            List<Earrange> eArrangeList = _context.Earranges.ToList();
            return new ResultUtils() { code = 200, message = eArrangeList };
        }

        public ResultUtils addEArrange(string token, Earrange data)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"添加考试安排---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (user.PositionId != 6) {
                AppLog.WriteError($"添加考试安排---非教务处账户{token}尝试登录", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (data == null)
            {
                AppLog.WriteError($"添加考试安排---审批表信息为空", true);
                return new ResultUtils() { code = 500, message = "Earrange为空" };
            }
            string[] time = data.EarrangeTime.Split('-');
            Schedule pd1 = _context.Schedules.Where(s =>
            s.ScheduleWeekly == time[1] &&
            s.ScheduleWeek == time[2] &&
            s.ScheduleWhat == time[3] &&
            s.Semester == int.Parse(time[0]) &&
            (s.ClassId == data.ClassId || s.WorkerId == data.WorkerId || s.SchedulePlace == data.SchedulePlace)
            ).FirstOrDefault();
            Earrange pd2 = _context.Earranges.Where(e=>
            e.EarrangeTime == data.EarrangeTime &&
            (e.ClassId == data.ClassId || e.WorkerId == data.WorkerId || e.SchedulePlace == data.SchedulePlace)
            ).FirstOrDefault();
            if (pd1 != null || pd2 != null)
            {
                AppLog.WriteError($"添加考试安排---老师或班级或教室占用，安排失败", true);
                return null;
            }
            data.EarrangeId = 0;
            _context.Earranges.Add(data);
            int result = _context.SaveChanges();
            if(result == 0)
            {
                AppLog.WriteError($"添加考试安排---{token}考试安排失败", true);
                return null;
            }
            AppLog.WriteInfo($"添加考试安排---考试安排成功", true);
            return new ResultUtils() { code=200, message="考试安排添加成功"};
        }

        public ResultUtils updateEArrange(string token, Earrange data)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"修改考试安排---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"修改考试安排---非教务处账户{token}尝试登录", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (data == null)
            {
                AppLog.WriteError($"修改考试安排---审批表信息为空", true);
                return new ResultUtils() { code = 500, message = "Earrange为空" };
            }
            string[] time = data.EarrangeTime.Split('-');
            Schedule pd1 = _context.Schedules.Where(s =>
            s.ScheduleWeekly == time[1] &&
            s.ScheduleWeek == time[2] &&
            s.ScheduleWhat == time[3] &&
            s.Semester == int.Parse(time[0]) &&
            (s.ClassId == data.ClassId || s.WorkerId == data.WorkerId || s.SchedulePlace == data.SchedulePlace)
            ).FirstOrDefault();
            Earrange pd2 = _context.Earranges.Where(e =>
            e.EarrangeTime == data.EarrangeTime &&
            (e.ClassId == data.ClassId || e.WorkerId == data.WorkerId || e.SchedulePlace == data.SchedulePlace)
            ).FirstOrDefault();
            if (pd1 != null || pd2 != null)
            {
                AppLog.WriteError($"修改考试安排---老师或班级或教室占用，安排修改失败", true);
                return null;
            }
            Earrange earrange = _context.Earranges.Find(data.EarrangeId);
            earrange.WorkerId = data.WorkerId;
            earrange.SchedulePlace = data.SchedulePlace;
            earrange.Courseid = data.Courseid;
            earrange.CourseNatureId = data.CourseNatureId;
            earrange.EarrangeTime = data.EarrangeTime;
            earrange.ClassId = data.ClassId;
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"修改考试安排---{token}考试安排修改失败", true);
                return null;
            }
            AppLog.WriteInfo($"修改考试安排---考试安排修改成功", true);
            return new ResultUtils() { code = 200, message = "考试安排修改成功" };
        }

        public ResultUtils GetEArrangesByToken(string token,string classId)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询考试安排---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (user.PositionId == 1)
            {
                Model.Models.Student student = _context.Students.Where(x => x.StuId == user.Username).FirstOrDefault();
                if (student == null) {
                    AppLog.WriteError($"查询考试安排---账户{token}尝试登录但登录失败", true);
                    return new ResultUtils() { code = 500, message = "Token错误" };
                }
                IEnumerable<Earrange> earranges = _context.Earranges.Where(e => e.ClassId == student.ClassId).ToList();
                return new ResultUtils() { code = 200, message = earranges };
            }
            else {
                Worker worker = _context.Workers.Where(x => x.WorkerId == user.Username).FirstOrDefault();
                if (worker == null)
                {
                    AppLog.WriteError($"查询考试安排---账户{token}尝试登录但登录失败", true);
                    return new ResultUtils() { code = 500, message = "Token错误" };
                }
                IEnumerable<Earrange> earranges = _context.Earranges.Where(e => e.ClassId == classId).ToList();
                return new ResultUtils() { code = 200, message = earranges };
            }

        }
    }
}
